import json
import urllib.request
import urllib


def getUrl(url):
    req = urllib.request.Request(url)
    re = urllib.request.urlopen(req)
    str2 = re.read().decode("utf-8")
    js = json.loads(str2)
    return js


def getData(url):
    js = getUrl(url)
    for i in range(10):
        problem = js['data']['data'][i]['content_p']
        help_id = js['data']['data'][i]['organization_help_id']
        company = js['data']['data'][i]['organization']['organization_name']

        url1 = "https://api.goingtowin.com/v1/government/organization/help/timeline/index?" \
               "help_id=%s&no_page=1&platform=pc&city_id=330700&token=2EBbNQvEuexXKFCkmH"
        js1 = getUrl(url1 % help_id)

        if js1['data'][0]['content'] is None:
            continue

        # print("公司：", company)
        # print("问题：", problem)

        writeFile("公司名称：" + company)
        writeFile("企业诉求：" + problem)

        num = len(js1['data'])
        for j in range(num):
            if js1['data'][j]['content'] is None or js1['data'][j]['dealer'] is None:
                break
            resu = js1['data'][j]['content'][0]['value']
            resuP = js1['data'][j]['dealer']['fullname']
            # print("方法：", resu)
            # print("处理人：", resuP)
            writeFile("处理方法：" + resu)
            writeFile("处理人：" + resuP)
        writeFile("\n"+"\n")


def writeFile(data):
    with open(r"E:\filework\c.txt", "a", encoding="utf-8") as f:
        f.write(data + "\n")
        f.close()


url = "https://api.goingtowin.com/v1/government/organization/help/myarea/index?" \
      "page=%d&platform=pc&city_id=330700&token=2EBbNQvEuexXKFCkmH"
for num in range(1, 5):
    getData(url % num)






